/**
  ******************************************************************************
  * @file    poolmanage.h
  * @author  Dragon.Chen
  * @version V1.0.2
  * @date    11-26-2014
  * @brief   内存管理
  ******************************************************************************
  *
  * <h2><center>&copy; COPYRIGHT 2014 上海今中网络科技有限公司 </center></h2>
  ******************************************************************************
  */

#ifndef __POOL__H__
#define __POOL__H__

#include "includes.h"

/*********************************** sock_buffer *******************************
 * 管理sock_buffer数据结构:
 *
 * NAME: SOCK_BUFFER_MANAGE_T   |<------------------- nSockBufferList ----------------->|
 * +--------------------+       NAME: SOCK_BUFFER_LIST_T    NAME: SOCK_BUFFER_LIST_T
 * | sockBufferListHead +------>+-----------------+         +-----------------+
 * | nSockBufferList    | +-----| sockBufferhead  | +-------| sockBufferhead  |
 * | maxSockBufferList  | | +---| freeSockBuffer  | +-------| freeSockBuffer  |     ......
 * | nSockBuffer        | | |   | nUsedSockBuffer | |       | nUsedSockBuffer |
 * +--------------------+ | |   | next            +-|------>+ next            +---->
 *                        | |   +-----------------+ |       +-----------------+
 *                        | |                       |
 *                        | |   NAME:SOCK_BUFFER_T  |       NAME:SOCK_BUFFER_T
 *              -+-       +-|-->+----------------+  +------>+----------------+      ......
 *               |          |   | buffer         |          | buffer         |
 *               |          |   | bufferLength   |          | bufferLength   |
 *               |          |   | bufferPosition |          | bufferPosition |
 *               |          |   | data           +--+       | data           +--+
 *               |          +-->+----------------+<-+       +----------------+<-+
 *               |              | buffer         |          | buffer         |
 *           nSockBuffer        | bufferLength   |          | bufferLength   |
 *               |              | bufferPosition |          | bufferPosition |
 *               |              | data           +--+       | data           +--+
 *               |              +----------------+<-+       +----------------+<-+
 *               |              | buffer         ||         | buffer         |
 *               |              | bufferLength   ||         | bufferLength   |
 *               |              | bufferPosition ||         | bufferPosition |
 *               |              | data           +--+       | data           +--+
 *               |              +----------------+<-+       +----------------+<-+
 *              -+-             ......                      ......
 *
 ******************************************************************************/

typedef struct _sock_buffer_list_t SOCK_BUFFER_LIST_T;
struct _sock_buffer_list_t
{
    SOCK_BUFFER_T       *sockBufferhead;        // sock_buffer头部
    SOCK_BUFFER_T       *freeSockBuffer;        // sock_buffer可用头部
    int                  nUsedSockBuffer;       // 已被使用的sock_buffer
    SOCK_BUFFER_LIST_T  *next;                  // 下个结点
};

typedef struct _sock_buffer_manage_t SOCK_BUFFER_MANAGE_T;
struct _sock_buffer_manage_t
{
    SOCK_BUFFER_LIST_T  *sockBufferListHead;    // sock_buffer_list链表的头部
    int                  nSockBufferList;       // sock_buffer_list链表的长度
    int                  maxSockBufferList;     // sock_buffer_list链表的上限值(大于此值，有空闲内存，就释放)
    int                  nSockBuffer;           // 每次分配sock_buffer动态数组的大小
};

/**
  * @brief  初始化数据sock_buffer
  * @param  arg1:sockBufferManage
  * @retval void
  */
void init_sock_buffer(SOCK_BUFFER_MANAGE_T *sockBufferManage);

/**
  * @brief  获取sockBuffer
  * @param  arg1:sockBufferManage
  * @retval 成功返回获取sockBuffer，失败返回NULL
  * @attention 请勿必重复申请，init_sock_buffer和free_sock_buffer必须成对使用，用完一定要释放
  */
SOCK_BUFFER_T *get_sock_buffer(SOCK_BUFFER_MANAGE_T *sockBufferManage);

/**
  * @brief  回收sockBuffer
  * @param  arg1:sockBufferManage  arg2:待释放的sockBuffer
  * @retval 成功返回获取sockBuffer，失败返回NULL
  */
void free_sock_buffer(SOCK_BUFFER_MANAGE_T *sockBufferManage, SOCK_BUFFER_T *sockBuffer);

/**
  * @brief  释放所有申请的内存
  * @param  arg1:head
  * @retval void
  */
void finalize_sock_buffer(SOCK_BUFFER_MANAGE_T *sockBufferManage);


/***************************** client data *****************************/
typedef struct _client_data_manage_t  CLIENT_DATA_MANAGE_T;
struct _client_data_manage_t
{
    CLIENT_DATA_T      *clientDataHead;         // clientData头部
    CLIENT_DATA_T      *freeClientData;         // 指向未使用的头部
    CLIENT_DATA_T      *usedClientData;         // 已链接的链表头部
    int                 nClientData;            // clientData的数据个数
    int                 nUsedClientData;        // 已经使用的clientData
};

/**
  * @brief  初始化用户数据
  * @param  arg1:clientDataManage
  * @retval void
  */
void init_client_data(CLIENT_DATA_MANAGE_T *clientDataManage);

/**
  * @brief  获取空闲的用户数据
  * @param  arg1:clientDataManage
  * @retval 成功返回tempClientData，失败返回NULL
  */
CLIENT_DATA_T *get_client_data(CLIENT_DATA_MANAGE_T *clientDataManage);

/**
  * @brief  释放的用户数据
  * @param  arg1:clientDataManage  arg2:clientData
  * @retval void
  */
void free_client_data(CLIENT_DATA_MANAGE_T *clientDataManage, CLIENT_DATA_T *clientData);

/**
  * @brief  释放所有的用户数据
  * @param  arg1:clientDataManage  arg2:clientData
  * @retval void
  */
void finalize_client_data(CLIENT_DATA_MANAGE_T *clientDataManage);


/***************************** certificate *****************************/
typedef struct _certificate_manage_t
{
    CERTIFICATE_T      *certificateHead;
    CERTIFICATE_T      *freeCertificate;
    int                 nCertificate;
} CERTIFICATE_MANAGE_T;

/**
  * @brief  初始化证书
  * @param  arg1:clientDataManage  arg2:clientData
  * @retval void
  */
void init_certifcate(CERTIFICATE_MANAGE_T *certificateManage);

/**
  * @brief  获取证书
  * @param  arg1:certificateManage
  * @retval void
  */
static inline CERTIFICATE_T *
get_certificate(CERTIFICATE_MANAGE_T *certificateManage)
{
    CERTIFICATE_T *tempCertificate;

    if (certificateManage->freeCertificate == NULL)
        return NULL;

    tempCertificate = certificateManage->freeCertificate;
    certificateManage->freeCertificate = tempCertificate->next;

    return tempCertificate;
}

/**
  * @brief  释放证书
  * @param  arg1:certificateManage  arg2:certificate
  * @retval void
  */
static inline void
free_certificate(CERTIFICATE_MANAGE_T *certificateManage, CERTIFICATE_T *certificate)
{
    if (certificate == NULL)
        return;

    ZeroMemory(certificate, sizeof(CERTIFICATE_T));
    certificate->next = certificateManage->freeCertificate;
    certificateManage->freeCertificate = certificate;
}

/**
  * @brief  释放所有的内存
  * @param  arg1:certificateManage
  * @retval void
  */
void finalize_certificate(CERTIFICATE_MANAGE_T *certificateManage);


#endif

/******************* (C) COPYRIGHT 2014 上海今中网络科技有限公司 *****END OF FILE****/




